﻿using Edu.Abstracts;
using Edu.Contracts;
using Edu.Entitys;
using System;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Edu.Services
{
    public class StudyTaskCrudAppService : AsyncCrudAppService<AppStudyTask, StudyTaskDto, Guid, PagedAndSortedResultRequestDto,
                            StudyTaskCreateUpdateDto, StudyTaskCreateUpdateDto>, IStudyTaskCrudAppService
    {
        private IAppUserRepository appUsers;

        public StudyTaskCrudAppService(IRepository<AppStudyTask, Guid> repository, IAppUserRepository appUsers) : base(repository)
        {
            this.appUsers = appUsers;
        }


        public override async Task<StudyTaskDto> CreateAsync(StudyTaskCreateUpdateDto input)
        {
            await CheckCreatePolicyAsync();



            var teacher = await appUsers.FindByPhoneNumberAsync(input.TeacherPhoneNum);
            if (teacher == null)
            {
                throw new UserFriendlyException("教师不存在");
            }


            var student = await appUsers.FindByPhoneNumberAsync(input.StudentPhoneNum);
            if (student == null)
            {
                throw new UserFriendlyException("学生不存在");
            }

            input.StudentId = student.Id;
            input.TeacherId = teacher.Id;

            var entity = MapToEntity(input);

            await Repository.InsertAsync(entity, autoSave: true);
            return MapToEntityDto(entity);
        }

        public override async Task<StudyTaskDto> UpdateAsync(Guid id, StudyTaskCreateUpdateDto input)
        {
            await CheckUpdatePolicyAsync();

            var entity = await GetEntityByIdAsync(id);


            var teacher = await appUsers.FindByPhoneNumberAsync(input.TeacherPhoneNum);
            if (teacher == null)
            {
                throw new UserFriendlyException("教师不存在");
            }


            var student = await appUsers.FindByPhoneNumberAsync(input.StudentPhoneNum);
            if (student == null)
            {
                throw new UserFriendlyException("学生不存在");
            }

            input.StudentId = student.Id;
            input.TeacherId = teacher.Id;


            MapToEntity(input, entity);
            await Repository.UpdateAsync(entity, autoSave: true);

            return MapToEntityDto(entity);
        }



        protected override AppStudyTask MapToEntity(StudyTaskCreateUpdateDto createInput)
        {
            var entity = ObjectMapper.Map<StudyTaskCreateUpdateDto, AppStudyTask>(createInput);
            entity.Content = new StudyTaskContent();
            SetIdForGuids(entity);
            ObjectMapper.Map<StudyTaskCreateUpdateDto, StudyTaskContent>(createInput, entity.Content);
            return entity;
        }

        protected override void MapToEntity(StudyTaskCreateUpdateDto updateInput, AppStudyTask entity)
        {
            if (updateInput is IEntityDto<Guid> entityDto)
            {
                entityDto.Id = entity.Id;
            }

            ObjectMapper.Map(updateInput, entity);
            ObjectMapper.Map(updateInput, entity.Content);
        }

    }
}
